#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：Incremental_synchronization_service.py
@Author  ：关依林
@Date    ：2023/7/25 16:37 
'''
import datetime

from common.dir_config import ConfigHandler
from testcases.ProductGrading_Strategy.ProductLibrary.initialize_strategy_service import PolicyConditionMethod
from testcases.ProductGrading_Strategy.public_dispatch_way import *
from testcases.ProductGrading_Strategy.invoking_interface import *
from utils.logging_tool.logging_control import Logging
from utils.readFileUtils.yamlControl import GetYamlData


class IncrementStrategyVerify:

    def __init__(self):
        self.strategyConfiguration = GetYamlData(file_dir=ConfigHandler.strategy_dir + "strategy_case02.yaml").get_yaml_data()

    def incrementSkuVerify(self):
        """
        验证思路：
        1.获取增量服务策略最新执行时间，jobId:854
        2.通过最新执行时间获取增量表t_product_incr的SKU数据
        3.获取开启状态的策略ID
        4.遍历策略，获取策略规则
        5.组装策略SQL并指定增量SKU进行查询
        6.将满足策略的SKU与策略明细SKU进行比对
        """
        startTime, endTime = getTimedTask(jobId=854)
        # useLogHandler("INFO").info("开始获取增量Sku数据, 增量范围：" + startTime + "-" + endTime)
        # 获取增量SKU数据
        IncrementSkuList = getIncrementSku(startTime, endTime)
        # 获取启用状态策略
        strategyIDList = getStrategyID()

        # 连接数据库
        mysqlCursorProduct = connectMysql(dbName="mysql_db_strategy_base_product")
        mysqlCursorCategory = connectMysql(dbName="mysql_db_strategy_base_category")
        # 获取SQL集合
        conditionSqlSet = self.strategyConfiguration["sql"]
        comparison = self.strategyConfiguration["comparison"]

        # 遍历所有启用状态策略
        for id in strategyIDList:
            # 获取策略规则
            messageList = getStrategyRule(categoryId = id["id"])
            sku = ()
            for condition in messageList:
                if not sku:
                    sku = processingLogicIncrement(conditionSqlSet, condition, mysqlCursorProduct, mysqlCursorCategory, sku, IncrementSkuList)
                else:
                    sku = processingLogicIncrement(conditionSqlSet, condition, mysqlCursorProduct, mysqlCursorCategory, sku, IncrementSkuList)

            # 获取策略关联的SKU数据
            strategyResult = tuple(getStrategyResult(comparison, id["id"]))
            print(strategyResult)
            sku = set(sku)
            strategyResult = set(strategyResult)
            abnormalRes = tuple(sku - strategyResult)
            abnormalRes = tuple(strategyResult - sku)
            # for i in abnormalRes:
            #     # useLogHandler("ERROR").error("策略ID：" + f"{id['id']}" + "," + "SKU：" + i[0] + "," + "该增量变更SKU策略匹配异常")

if __name__ == '__main__':
    IncrementStrategyVerify().incrementSkuVerify()